package engine.newUi
{
	import engine.control.ControlCreater;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.ResPath;
	import engine.data.RoleVo;
	import engine.data.SceneObject;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.display.newAttack.action.PhyCloseAttack;
	import engine.event.SoundEvent;
	import engine.flag.PlayerActionFlag;
	import engine.flag.ResourcesPath;
	import engine.logic.DieHelpLogic;
	import engine.logic.DoubleClickManager;
	import engine.logic.LogSendLogic;
	import engine.logic.RequestForCost;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.KungfuItem;
	import engine.ui.AlertUI;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	import newx.BaseObject;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.MouseHandle;

	/**
	 *功法 
	 * @author yanghr
	 * 
	 */	
	public class FuncPowerLogic
	{
		private static var _self:FuncPowerLogic;
		
		/**
		 *属性名 
		 */		
		private const DATAOBJ:String="dataobj";
		private const INDEX:String="index";
		private const ITEM:String="item";
		private const TXT:String="titleTxt2";
		private const LVL:String="infoTxt5";
		private const BIND:String="bind";
		private const NAME_TITLE:String="T1";
		private const LEVEL_TITLE:String="T3";
		private const LOCK_DATA:String="lockIndex";
		
		private const BIND_LEVEL:String="bind_level";
		
		private const CONFIGID:String="ConfigID";
		private const VIEW_ID:String="_viewid";
		private const PHOTO:String="EffectivePhoto"
		
		private const NORMAL:int	=	1;
		private const ROLL:int		=	2;
		private const DISABLE:int	=	3;
		private const DISABLE_ROLL:int	=	4;
		
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private var _mouse:MouseHandle;
		private var _doubleClickManager:DoubleClickManager;
		private var _kungfuitem:KungfuItem;
		private var _ui:FuncPowerUI;
		private var _rollItem:MovieClip;
		private var _levelObj:Object;

		private var _bindFlag:int=-1;
		private var _bindKungfu:int=-1;
		private var _bindKungfuTool:int=-1;
		private var _bindKungfuToolCount:int=-1;
		private var _bindKungfuCount:int=-1;
		private var _bindReminCountFlag:int=-1;
		private var _bindPowerFlag:int=-1
		private var _eqObjViewflag:int;
		/**
		 *装备长度 
		 */		
		private var _len:int;
		
		private var _unlock:int
		private var _lockIndex:int
		
		private var _roleArr:Array=[];
		private var _currentSelectedM:MovieClip;
		private var photoMc:MovieClip;
		private var _glow:GlowFilter = new GlowFilter(0x0a2529, 1, 2, 2, 3, BitmapFilterQuality.LOW);
		public function get ui():FuncPowerUI
		{
			if(_ui==null)
			{
				_ui=new FuncPowerUI
			}
			return _ui;
		}
		public static function getInstance():FuncPowerLogic
		{
			if(_self==null)
			{
				_self=new FuncPowerLogic();
			}
			return _self;
		}
		public function FuncPowerLogic()
		{
		}

		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_mouse=MouseHandle.getInstance();
			_doubleClickManager	= DoubleClickManager.getInstance();
			_kungfuitem=new KungfuItem;
			_ui.composeBtn.label=StaticIni.getSwfLangStr("Kungfu_UIString2");//"一键合成";
			_ui.gotoBtn.label=StaticIni.getSwfLangStr("Kungfu_UIString3")//");去扔色子";
			_ui.exchangeBtn.htmlStrByBorder	=	_tip.getUnderLineHtml(StaticIni.getSwfLangStr("Kungfu_UIString4"),"d");//"介绍和兑换";
			_ui.exchangeBtn.mouseEnabled=_ui.exchangeBtn.buttonMode=true;
			_ui.exchangeBtn.addEventListener(MouseEvent.ROLL_OVER,function(e:Event):void{
				_ui.exchangeBtn.htmlStrByBorder	=	_tip.getUnderLineHtml(
					TipsMainLogic.instance.getColorHtml(_ui.exchangeBtn.text,"00ff00")
					,"d");
			});
			_ui.exchangeBtn.addEventListener(MouseEvent.ROLL_OUT,function(e:Event):void{
				_ui.exchangeBtn.htmlStrByBorder	=	_tip.getUnderLineHtml(StaticIni.getSwfLangStr("Kungfu_UIString4"),"d");
			});
			
//			for (var k:int = 0; k < _ui.exchangeBtn.numChildren; k++) 
//			{
//				var mc:DisplayObject=_ui.getChildAt(k);
//				if(mc is TextField)
//				{
//					(mc as TextField).mouseEnabled=true;
//					break;
//				}
//			}
			
			
			var tempObj:Object=StaticIni.getIniObj("PlayerLevelBox");
			_levelObj=new Object;
			for  (var j:String in tempObj) 
			{
				_levelObj[tempObj[j]]=j
			}
			resetRoleArr()
			initPartnerList();
			forEachKungfuOn(function(m:MovieClip,i:int):void{
				m[INDEX]=i;
				m[NAME_TITLE].htmlText="";
				(m[NAME_TITLE] as TextField).autoSize=TextFieldAutoSize.CENTER;
				(m[LEVEL_TITLE] as TextField).autoSize=TextFieldAutoSize.CENTER;
				m[LEVEL_TITLE].htmlText="";
				m[NAME_TITLE].mouseEnabled=m[LEVEL_TITLE].mouseEnabled=false;
				m.addEventListener(MouseEvent.CLICK,click);
				m.addEventListener(MouseEvent.ROLL_OVER,rollOver);
				m.addEventListener(MouseEvent.ROLL_OUT,rollout);
			});
			var length:int=_ui.packGroup.col*_ui.packGroup.row;
			for (var i:int = 0; i < length; i++) 
			{
				var mc:MovieClip=_ui.packGroup.getItemByIndex(i);
				mc[INDEX]=i+1;
				mc[VIEW_ID]=View.VIEWPORT_KUNGFUTOOL;
				mc[NAME_TITLE].htmlText="";
				(mc[NAME_TITLE] as TextField).autoSize=TextFieldAutoSize.CENTER;
				mc[LEVEL_TITLE].htmlText="";
				(mc[LEVEL_TITLE] as TextField).autoSize=TextFieldAutoSize.CENTER;
				mc[NAME_TITLE].mouseEnabled=mc[LEVEL_TITLE].mouseEnabled=false;
				mc.addEventListener(MouseEvent.CLICK,click)
				mc.addEventListener(MouseEvent.ROLL_OVER,rollOver);
				mc.addEventListener(MouseEvent.ROLL_OUT,rollout);
			}
			_doubleClickManager.addDoubleClick( FuncPowerLogic, singleClick, doubleClick );
			_currentSelectedM=ui.actorGroup.getItemByIndex(0);
		}
		private function resetRoleArr():void
		{
			_roleArr=new Array();
			_roleArr[0]=_role;
		}
		public function	BeforeShow():void
		{
			addAllEventListener()
			if(currentSelectedM[INDEX]==0)
			{
				//				var mc:MovieClip=_ui.actorGroup.getItemByIndex(0);
				//				mc[TXT].htmlStr=role.getPlayerName()+"等级："+role.getPlayerLevel();
				refreshList(_role)
				//				refreshBlind()
				//				refreshUI()
			}
			refreshUI();
			forEachKungfuOn(function(mc:MovieClip,i:int):void{
				if(mc[ITEM]&&mc[ITEM].content)
					(mc[ITEM].content as MovieClip).play()
			})
			ui.packGroup.getItemForEach(function(mc:MovieClip):void{
				if(mc[ITEM]&&mc[ITEM].content)
					(mc[ITEM].content as MovieClip).play()
			})
		}
		public function	AfterHide():void
		{
			removeAllEventListener();
			forEachKungfuOn(function(mc:MovieClip,i:int):void{
				if(mc[ITEM]&&mc[ITEM].content)
				(mc[ITEM].content as MovieClip).stop()
			})
			ui.packGroup.getItemForEach(function(mc:MovieClip):void{
				if(mc[ITEM]&&mc[ITEM].content)
				{
					(mc[ITEM].content as MovieClip).stop()
					removeDataToMc(mc,mc[INDEX])
				}
			})
		}
		/**
		 *按钮毁掉 
		 * @param e
		 * 
		 */		
		private function clickBtnHandle(e:Event):void
		{
			switch(e.currentTarget)
			{
				case _ui.composeBtn:
				{
					AlertUI.showAlert(" ",StaticIni.getSwfLangStr("Kungfu_UIString8"),AlertUI.YES|AlertUI.NO,function createGuild( t:int, str:String, clickData:* ):void
					{
						if(AlertUI.YES == t )
						{
							SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDEXP_KUNGFU_AUTO);
							_model.dispatchEvent(new SoundEvent(SoundEvent.COMBINE_ITEMS));
						}
					}
					)
					break;
				}
				case _ui.exchangeBtn:
				{
					ExChangePowerLogic.getInstance().ui.showOrHide();
					break;
				}
				case _ui.gotoBtn:
				{
					if(!DiceLogic.getInstance().ui.created)
						DiceLogic.getInstance().ui.showOrHide();
					close(null);
					break;
				}
					
				default:
				{
					break;
				}
			}
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
		private const TOOLSIZE_PRO:String="KungFuToolBoxSize";
		public function addAllEventListener():void
		{
			if( _bindFlag == -1 )
			{
				_bindFlag		=	_bind.BindView(_model.scene,View.VIEWPORT_PARTNER,OnAddPartner,OnRemovePartner);
			}
			if( _bindKungfuTool == -1 )
			{
				_bindKungfuTool		=	_bind.BindView(_model.scene,View.VIEWPORT_KUNGFUTOOL,OnAddTool,OnRemoveTool);
			}
			if(_bindKungfuToolCount	==	-1)
			{
				_bindKungfuToolCount	 = _bind.BindProp(_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL),TOOLSIZE_PRO,this,"toolunlock")
			}
			if(_bindReminCountFlag==-1)
			{
				_bindReminCountFlag= _bind.BindProp(_role,"DefaultCount",this,"remainCount")
			}
			_ui.composeBtn.addEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.gotoBtn.addEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.exchangeBtn.addEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
		}
		public function removeAllEventListener():void
		{
			if( _bindFlag != -1 )
			{
				_bindFlag		=	_bind.UnbindView(View.VIEWPORT_PARTNER,_bindFlag);
				resetRoleArr();
			}
			if( _bindKungfuTool != -1 )
			{
				_bindKungfuTool		=	_bind.UnbindView(View.VIEWPORT_KUNGFUTOOL,_bindKungfuTool);
			}
			if(_bindKungfuToolCount != -1)
			{
				_bindKungfuToolCount	 = _bind.UnbindProp(_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL),"KungFuToolBoxSize",_bindKungfuToolCount)
			}
			if(_bindReminCountFlag != -1)
			{
				_bindReminCountFlag	 = _bind.UnbindProp(_role,"DefaultCount",_bindReminCountFlag)
			}
			_ui.composeBtn.removeEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.gotoBtn.removeEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.exchangeBtn.removeEventListener(MouseEvent.CLICK,clickBtnHandle);
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close);
		}
		public function set powerCount(value:int):void
		{
			_ui.funcCountLab.htmlStr=StaticIni.getSwfLangStrVar("Kungfu_UIString6",[value])//"功力： "+value;
		}
		public function set remainCount(value:int):void
		{
			_ui.leavCountLab.htmlStr=StaticIni.getSwfLangStrVar("Kungfu_UIString5",[value])//"残卷数量："+value;
		}
		public function set toolunlock(value:int):void
		{
			var $this:Object=this;
//			_ui.packGroup.getItemForEach(lock,[value])
			var length:int=_ui.packGroup.col*_ui.packGroup.row;
			lock(value,length,e);
			function e(i:int):MovieClip
			{
				return $this._ui.packGroup.getItemByIndex(i-1);
			}
		}
		private function lock(value:int,length:int,func:Function):void
		{
			for (var i:int = 1; i <=length; i++) 
			{
				var mc:MovieClip=func(i)//_ui.packGroup.getItemByIndex(i);
				mc.gotoAndStop(i>value?DISABLE:NORMAL)
				if(mc.currentFrame==DISABLE)
				{
					mc[LOCK_DATA]=_levelObj[i];
				}
			}
		}
		public function set kungfuUnlock(value:int):void
		{
			lock(value,_len,getKungfubyIndex);
			
		}
		private function getKungfuViewId(partnerID:int):int
		{
			var arr:Array=MyPropertyLogic.getInstance().getPartnerViewsById(partnerID);
			if(arr&&arr.length)
			return arr[4]		
			return 0;
		}
		private function getKungfubyIndex(index:int):MovieClip
		{
			if(_ui.hasOwnProperty("mc_"+index))
				return _ui["mc_"+index];
			return null;
		}
		/**
		 *选择伙伴 
		 * @param e
		 * 
		 */		
		private function actorClickHandle(e:Event):void
		{
			var target:DisplayObject=e.target as DisplayObject
			if(e.target is TextField)
			{
				target=target.parent;
			}
			//			currentSelected=proUI.actorGroup.geti
			if(target==currentSelectedM||!_roleArr[target[INDEX]])return ;
			//			(e.target as MovieClip).gotoAndStop(2);
			
			currentSelectedM=target as MovieClip;
			
		}
		public function get currentSelectedM():MovieClip
		{
			return _currentSelectedM;
		}
		
		public function set currentSelectedM(value:MovieClip):void
		{
			if(_currentSelectedM&&_currentSelectedM==value)
			{
				return 
				//				_currentSelectedM.filters=null;
			}
			_currentSelectedM.gotoAndStop(1)
			if(_bindPowerFlag != -1)
			{
				_bindPowerFlag	 = _bind.UnbindProp(currentSelectedM[DATAOBJ],"PlayerKungFuTotalExp",_bindPowerFlag)
			}
			_currentSelectedM = value;
			if(_currentSelectedM)
			{
				_currentSelectedM.gotoAndStop(3)
				refreshUI();
				//				_currentSelectedM.filters=[new GlowFilter()]
			}
		}
		private function refreshUI():void
		{
			clearAllKungFu();
			var vo:BaseObject=		currentSelectedM[DATAOBJ];
			var model:String=vo.GetProperties(ActorPropertyUI.MODEL);
			photo=model;
			refreshBind();
		}
		private function removeKungFu():void
		{
			if(_eqObjViewflag)
			{
				if( _bindKungfu != -1 )
				{
					_bindKungfu		=	_bind.UnbindView(_eqObjViewflag,_bindKungfu);
				}
				if(_bindKungfuCount != -1)
				{
					_bindKungfuCount	 = _bind.UnbindProp(_model.scene.GetView(_eqObjViewflag),"KungFuBoxSize",_bindKungfuCount)
				}
			}
		}
		public function get isHasKungfu():Boolean
		{
			var kungfuTool:Object=_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL).Properties;
			for  (var j:String in kungfuTool) 
			{
				if(Number(j))
				{
					return true;
				}
			}
				return false;
		}
		public function isAllViewHasEmpty():Boolean
		{
			var roleKungfu:View=_model.scene.GetView(View.VIEWPORT_KUNGFU);
			if(!roleKungfu)
				return false;
			else
			{
				if(isViewIdHasEmpty(roleKungfu))
					return true;
			}
			var obj:Object=_model.scene.GetView(View.VIEWPORT_PARTNER).Properties;
			for (var i:String in obj) 
			{
				if(Number(i))
				{
					if(isViewIdHasEmpty(_model.scene.GetView(getKungfuViewId(int(obj[i].GetPropertiesEx("ConfigID"))))))
						return true
				}
			}
			return false
		}
		private function isViewIdHasEmpty(view:View):Boolean
		{
//			var id:int//=getKungfuViewID();
//			id=viewid==0?View.VIEWPORT_KUNGFU:getKungfuViewId(viewObj.GetPropertiesEx("ConfigID"));
//			var view:View=_model.scene.GetView(id);
			var len:int;
			if(!view)return false;
			for (var i:String in view.Properties) 
			{
				if(Number(i))
					len++;
			}
			return len<view.GetPropertiesEx("KungFuBoxSize");
		}
		private function refreshBind():void
		{
			removeKungFu();	
			if(_bindKungfuCount	==	-1)
			{
				var id:int=getKungfuViewID();
				var view:View=_model.scene.GetView(id)
				if(!view)
				{
					kungfuUnlock=0;
				}
				else
				{
					_bindKungfuCount	 = _bind.BindProp(view,"KungFuBoxSize",this,"kungfuUnlock")
				}
			}
		
			if( _bindKungfu == -1 )
			{
				_bindKungfu		=	_bind.BindView(_model.scene,getKungfuViewID(),OnAddKungfu,OnRemoveKungfu);
			}
			if(_bindPowerFlag==-1)
			{
				_bindPowerFlag= _bind.BindProp(currentSelectedM[DATAOBJ],"PlayerKungFuTotalExp",this,"powerCount")
			}
		}
		private function clearAllKungFu():void
		{
			forEachKungfuOn(removeDataToMc);
		}
		/**
		 *function（mc:movieClip,index:i）:void{} 
		 * @param f
		 * 
		 */		
		private function forEachKungfuOn(f:Function):Array
		{
			var i:int=1
			var mc:MovieClip=getKungfubyIndex(i);
			var arr:Array=[]
			while(mc)
			{
				var o:Object=f(mc,i);
				if(o)
					arr.push(o);
				mc=getKungfubyIndex(++i)
			}
			_len=i-1;
			return arr
		}
		/**
		 *得到当前装备功法的viewid 
		 * @return 
		 * 
		 */		
		public function getKungfuViewID():int
		{
			//绑定装备
			if(currentSelectedM[INDEX]	==	0)
			{
				_eqObjViewflag=	View.VIEWPORT_KUNGFU
			}
			else 
			{
				 _eqObjViewflag=getKungfuViewId(_roleArr[currentSelectedM[INDEX]].GetPropertiesEx(CONFIGID));
			}
			return _eqObjViewflag;
		}
		private function OnAddTool(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			PropertiesValue.HandlePropertiesEx(VIEW_ID,View.VIEWPORT_KUNGFUTOOL)
			addToolByIndex(PropertiesValue,int(PropertiesName)-1);
		}
		private function addToolByIndex(propertyValue:ViewObject,index:int):void
		{
			var mc:MovieClip=_ui.packGroup.getItemByIndex(index);
			if(mc)
				addDataToMc(propertyValue,mc);
//			else
//				trace("Error~FuncPowerLogic.addToolByIndex(propertyValue, index)",index,"");
			
		}
		private function addDataToMc(value:ViewObject,mc:MovieClip):void
		{
			mc[DATAOBJ]=value;
			var nx:NxClip=mc[ITEM] as NxClip;
			if(!nx)
			{
				nx=mc[ITEM]=new NxClip;
				nx.mouseEnabled=nx.mouseChildren=false;
				if(mc.hasOwnProperty("img2"))
				{
					mc["img2"].addChild(nx);
//					nx.x=-14;
//					nx.y=-15;
//					trace("!!!!!!!FuncPowerLogic.addDataToMc(value, mc)"+"img2");
					
				}
				nx.isCache=true;
//				else
//					mc.addChild(nx);
				mc[NAME_TITLE].filters=[_glow];
				mc[[LEVEL_TITLE]].filters=[_glow];
//				mc.addEventListener(MouseEvent.ROLL_OVER,rollOver);
//				mc.addEventListener(MouseEvent.ROLL_OUT,rollout);
				mc.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag)
			}
			nx.source=value.GetPropertiesEx(PHOTO);
			_kungfuitem.dataObj=value;
			mc[NAME_TITLE].htmlText=_kungfuitem.getColorName();
			if(mc[BIND_LEVEL]==null)
			{
				mc[BIND_LEVEL]=function (lvl:int):void{
					_kungfuitem.dataObj=this[DATAOBJ]
					this[LEVEL_TITLE].htmlText=_kungfuitem.getLevel2();
				}
			}
//			mc[BIND]=_bind.BindProp(value,"KungFuLevel",mc,BIND_LEVEL);
			mc[BIND]	=_bind.BindPropInViewObj(value.parent as View,value.ObjKey,"KungFuLevel",BindFunc,null,[mc,BIND_LEVEL]);
//			mc[LEVEL_TITLE].htmlText=_kungfuitem.getLevel2();
			
			//String(value.GetPropertiesEx(PHOTO)).length<4?value.GetPropertiesEx(PHOTO):ResourcesPath.getPropsURL(value.GetPropertiesEx(PHOTO));
		}
		private function BindFunc(PropertiesName:String,PropertiesValue:*,pList:Array):void
		{
			var		Obj:Object	=	pList[0];
			var		value:*		=	pList[1];
			Obj[value](PropertiesValue);	
		}
		public function get rollItem():MovieClip
		{
			return _rollItem;
		}
		
		public function set rollItem(value:MovieClip):void
		{
			if(_rollItem)
			{
				if(_rollItem.currentFrame==ROLL)
					_rollItem.gotoAndStop(NORMAL);
				else if(_rollItem.currentFrame	==	DISABLE_ROLL)
					_rollItem.gotoAndStop(DISABLE)
			}
			_rollItem = value;
			if(_rollItem)
			{
				if(_rollItem.currentFrame==NORMAL)
					_rollItem.gotoAndStop(ROLL);
				else if(_rollItem.currentFrame	==	DISABLE)
					_rollItem.gotoAndStop(DISABLE_ROLL);
			}
		}
		private function click(e:MouseEvent):void
		{
			_doubleClickManager.onClick(FuncPowerLogic,e);
			
		}
		private function singleClick(e:Event):void
		{
			var mc:MovieClip=(e.target as MovieClip) as MovieClip;
			if((mc.currentFrame==DISABLE	||	mc.currentFrame==DISABLE_ROLL)	&&	mc[VIEW_ID])
			{
				RequestForCost.getInstance().request(4,openPackageByindex,mc[INDEX]);
			}
		}
		private function openPackageByindex(index:int):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_AFFIRM_CHARGE,4,index)
			LogSendLogic.getInstance().btnClickNumSend(22807)
		}
		private function doubleClick(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			var m2:MovieClip;
			if(!mc[DATAOBJ])return;
			if(mc[VIEW_ID])
			{
				var arr:Array=forEachKungfuOn(getKungfu);
				if(!arr||!arr.length)return 
				m2	=	arr[0];
			}
			else
			{
				m2	=	getFirstEmptyTool();
			}
			if(m2)
			{
				swap(mc[DATAOBJ],m2);
			}
			
		}
		private function alertNOSwallow():void
		{
			NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("Kungfu_UIString14")); 
		}
		private function getKungfu(mc:MovieClip,i:int):MovieClip
		{
			if(!mc[DATAOBJ]&&mc.currentFrame!=DISABLE)
			{
				return mc;
			}
			return null
		}
		private function getFirstEmptyTool():MovieClip
		{
			var len:int=_ui.packGroup.col*_ui.packGroup.row;
			for (var i:int = 0; i < len; i++) 
			{
				var m:MovieClip=getKungfu(_ui.packGroup.getItemByIndex(i),i);
				if(m)
					return m
			}
			return null
			
		}
		private function beginDrag(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			if(mc&&mc[DATAOBJ]&&mc[ITEM].content)
			{
				_mouse.setDrag(mc[ITEM].content,true,mc[DATAOBJ])
//				mc.addEventListener(MouseEvent.MOUSE_UP,endDrag)
				_ui.packGroup.addItemsEvent(MouseEvent.MOUSE_UP,endDrag);
				forEachKungfuOn(addE)
			}
			function addE(mc:MovieClip,i:int):void
				
			{
				mc.addEventListener(MouseEvent.MOUSE_UP,endDrag);
			}
		}
		private function endDrag(e:Event):void
		{
			_ui.packGroup.removeItemsEvent(MouseEvent.MOUSE_UP,endDrag);
			forEachKungfuOn(removeE)
			var mc:MovieClip=e.currentTarget as MovieClip;
			if(mc&&mc[DATAOBJ])
			{
				//融合
				swallow(_mouse.targetData as ViewObject,mc[DATAOBJ] as ViewObject)
			}
			else
			{
				//移动
				swap(_mouse.targetData as ViewObject,mc)
			}
			_mouse.removeDrag();
			function removeE(mc:MovieClip,i:int):void
				
			{
				mc.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
			}
		}
		private function swap(v1:ViewObject,mc:MovieClip):void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_MOVE_KUNGFU ,v1.GetPropertiesEx(VIEW_ID),int(v1.ObjKey),getTargetViewIdByMc(mc),int(mc[INDEX]));
		}
		private function getTargetViewIdByMc(mc:MovieClip):int
		{
			if(mc[VIEW_ID])
				return mc[VIEW_ID]
			return this.getKungfuViewID();
		}
		private function swallow(v1:ViewObject,v2:ViewObject):void
		{
			if(v1&&v2&&v1!=v2)
			{
//				KungFuQuality:3,
//				KungFuInitExp:60,
				if((v1.GetPropertiesEx("KungFuLevel")=="9"||v2.GetPropertiesEx("KungFuLevel")=="9")
					||(v1.GetPropertiesEx("KungFuQuality")==v2.GetPropertiesEx("KungFuQuality")&&KungfuItem.RED==int(v2.GetPropertiesEx("KungFuQuality"))))
				{
					alertNOSwallow();
//					trace("FuncPowerLogic.swallow(v1, v2)二笔等级为10",v1.GetPropertiesEx("KungFuLevel"),v2.GetPropertiesEx("KungFuLevel"));
					return 
				}
				if(v1.GetPropertiesEx("KungFuQuality")>v2.GetPropertiesEx("KungFuQuality"))
				{
					
					sendMsg(v2,v1,1);
				}
				else if(v1.GetPropertiesEx("KungFuQuality")<v2.GetPropertiesEx("KungFuQuality"))
				{
					sendMsg(v1,v2,2);
				}
				else
				{
					if(v1.GetPropertiesEx("KungFuLevel")>v2.GetPropertiesEx("KungFuLevel"))
					{
						sendMsg(v2,v1,1)
					}
					else if(v1.GetPropertiesEx("KungFuLevel")<v2.GetPropertiesEx("KungFuLevel"))
					{
						
						sendMsg(v1,v2,2)
					}
					else
					{
						if(v1.GetPropertiesEx("KungFuExp")<v2.GetPropertiesEx("KungFuExp"))
						{
							sendMsg(v1,v2,2)
						}
						else
						{
							sendMsg(v2,v1,1)
						}
					}
				}
			}
			else
			{
//				trace("Error~FuncPowerLogic.swallow(v1, v2)怎么可能是空的",v1==v2,v1,v2);
			}
			
			
			
		}
		
		private function sendMsg(s1:ViewObject,s2:ViewObject,type:int):void
		{
			var	title		:String			=	StaticIni.getSwfLangStr("31100");
			var kungfu		:KungfuItem		=	_kungfuitem//new KungfuItem(s1);
			_kungfuitem.dataObj=s1;
			var name1		:String			=	kungfu.getColorName();
			var expadd:int=kungfu.kungfuCurrentExp+kungfu.kungfuInitExp;
			kungfu.dataObj=	s2;
			var name2		:String			=	kungfu.getColorName();
			var	iniDesc		:String			=	getMainStr()//name2+"吞噬"+name1//StaticIni.getSwfLangStr("UIStr_1000485");
			var	yesBtn		:String			=	StaticIni.getSwfLangStr("AlertEx_16");
			var	noBtn		:String			=	StaticIni.getSwfLangStr("AlertEx_1");
			AlertUI.showAlert(title,iniDesc,AlertUI.YES|AlertUI.NO,createGuild,null/*,false,300,150*/);
			function createGuild( t:int, str:String, clickData:* ):void
			{
				if(AlertUI.YES == t )
				{
					sendMsg2();
				}
				
			}
			function sendMsg2():void
			{
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_ADDEXP_KUNGFU,s1.GetPropertiesEx(VIEW_ID),int(s1.ObjKey),s2.GetPropertiesEx(VIEW_ID),int(s2.ObjKey),type);
			}
			function getMainStr():String
			{
				var str1:String	=	"Kungfu_UIString12";//ame={@0:}将融合{@1:}获得{@2:}点经验
				var str2:String	=	"Kungfu_UIString9";//		name={@0:}将融合{@1:}获得{@2:}点经验，等级提升至{@3:}
				//s2吞噬s1;
				var lvl:int=kungfu.addExUptoLevel(expadd);
				if(lvl==kungfu.level)
					return StaticIni.getSwfLangStrVar(str1,[name2,name1,expadd])
				else
					return StaticIni.getSwfLangStrVar(str2,[name2,name1,expadd,lvl])
			}
		}
		private function rollOver(e:Event):void
		{
			var mc:MovieClip=e.currentTarget as MovieClip;
			rollItem=mc;
			if(mc&&mc[DATAOBJ])
				_tip.showMain(mc[DATAOBJ])
			else if((mc.currentFrame==DISABLE	||	mc.currentFrame==DISABLE_ROLL)&&!_ui.packGroup.contains(mc))
			{
				_tip.showMain(StaticIni.getSwfLangStrVar("Kungfu_UIString10",[mc[LOCK_DATA]]))
			}
		}
		private function rollout(e:Event):void
		{
			rollItem=null;
			_tip.hide();
		}
		private function removeToolByIndex(index:int):void
		{
			var mc:MovieClip=_ui.packGroup.getItemByIndex(index);
			removeDataToMc(mc);
		}
		private function removeDataToMc(mc:MovieClip,i:int=0):void
		{
			mc[DATAOBJ]=null;
			var nx:NxClip=mc[ITEM] as NxClip;
			if(nx)
			{
				nx.source=null;
				nx.clear();
			}
			mc[NAME_TITLE].htmlText=""
			mc[LEVEL_TITLE].htmlText=""//_kungfuitem.getLevel2();
		}
		private function OnRemoveTool(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			removeToolByIndex(int(PropertiesName)-1);
		}
		private function OnAddKungfu(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			PropertiesValue.HandlePropertiesEx(VIEW_ID,getKungfuViewID());
			addKungfuByIndex(PropertiesValue,int(PropertiesName));
		}
		private function addKungfuByIndex(propertyValue:ViewObject,index:int):void
		{
			var mc:MovieClip=getKungfubyIndex(index);
			addDataToMc(propertyValue,mc);
		}
		private function removeKungfuByIndex(index:int):void
		{
			var mc:MovieClip=getKungfubyIndex(index);
			removeDataToMc(mc);
		}
		private function OnRemoveKungfu(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			removeKungfuByIndex(int(PropertiesName));
		}
		private function OnAddPartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			if(_roleArr.indexOf(PropertiesValue)>-1)
				return ;
			_roleArr.push(PropertiesValue);
			if(PropertiesValue.ObjKey==currentSelectedM[INDEX])
			{
				refreshBind();
			}
			refreshList(PropertiesValue);
		}
		
		//删除可能一辈子都不会呗调用吧		
		private function OnRemovePartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_roleArr.splice(_roleArr.indexOf(PropertiesValue),1);
			//			[PropertiesValue.ObjKey]=null;
			if(PropertiesValue.ObjKey==currentSelectedM[INDEX])
			{
				refreshBind();
			}
			refreshList(PropertiesValue)
		}
		/**
		 *初始化伙伴列表 
		 * 
		 */		
		private function initPartnerList():void
		{
			var len:int		=	ui.actorGroup.col*ui.actorGroup.row
			//伙伴列表
			for (var i:int = 0; i <len ; i++) 
			{
				var mc:MovieClip	=	ui.actorGroup.getItemByIndex(i);
				//				mc[TXT]		=	new NxLabel();
				mc[TXT].mouseEnabled	=false
				//				mc.addChild(mc[TXT]);
				mc[INDEX]	=	i;
			}
		}
		public function set photo(value:String):void 
		{
			//			var url : String  = StageInfo.npcResPath + value + "_ani_0.swf";
			//			if( photoMc != null && url == actorVisual.name)
			//			{
			//				return;
			//			}
			var roleSkinIndex:int	=	0;
			var url:String	=	getUrl()
			url	=	ResPath.ACTORS + url;
			//			roleModel	=	ResPath.ACTORS + roleModel + value + ".swf";
			ui.actorView.source=url;
//			ResManager.loadResNoSave(url, url, onPhotoLoad,[url]);
			function getUrl():String
			{
				var head:String;
				var tails:String;
				if(currentSelectedM[INDEX]!=0)
				{
					head	=	"npc_";
					tails	=	".swf";
				}
				else
				{
					roleSkinIndex	=	ModelLocator.getInstance().scene.Role.GetPropertiesEx("ClotheModel");
					roleSkinIndex	=	roleSkinIndex  == 0?1:roleSkinIndex;
					head	=	"role_";
					tails	=	"_"+roleSkinIndex+".swf";					
				}
				return head+value+tails;
			}
			
		}
		public function get isToolHasEmpty():Boolean
		{
			var obj:Object=_model.scene.GetView(View.VIEWPORT_KUNGFUTOOL).Properties;
			var _len:int;
			for  (var i:String in obj) 
			{
				if(Number(i))
				{
					_len++;
				}
			}
			if(_len==obj[TOOLSIZE_PRO])
			return false
			return true;
		}
		public function get isMoneyEnough():Boolean
		{
			return _role.getGoldCoin()>=7000
		}
		public function get checkGuide():int
		{
			if(isHasKungfu&&isAllViewHasEmpty())//如果武学背包内有武学且有空位，则提示装备武学；
			{
//				trace("提示装备武学")
				return DieHelpLogic.KUNGFU_EQUIP;
			}
			if(isToolHasEmpty	&&	isMoneyEnough)//如果武学背包内没有武学且空位，则提示获取武学；
			{
//				trace("提示获取武学；");
				return DieHelpLogic.KUNGFU_GET;
			}
			//如果没有武学空位，不再提示武学；
			//如果不提示武学，进入阵法引导；
			return -1;
		}
		/**
		 *刷新伙伴列表 
		 * 
		 */		
		private function refreshList(view:ObjectData):Boolean
		{
			var $this:Object=this;
			//重置 好友列表
			if(_roleArr.length	!=	ui.actorGroup.row)
			{
				var skin:String=ui.actorGroup.viewSkin;
				//				var col:int=ui.actorGroup.col;
				var row:int=_roleArr.length;
				var x:Number=ui.actorGroup.x;
				var y:Number=ui.actorGroup.y;
				var pa:DisplayObjectContainer=ui.actorGroup.parent;
				var selectIndex:int=currentSelectedM[INDEX]
				//				ui.actorGroup
//				removeAllMcBlind();
				ui.actorGroup.removeEventListener(MouseEvent.CLICK,actorClickHandle,true);
//				ui.actorGroup.removeItemsEvent(MouseEvent.ROLL_OVER,grouprollOver)
				ui.actorGroup.parent.removeChild(ui.actorGroup)
				ui.actorGroup=null;
				//创建
				ui.actorGroup=ControlCreater.getInstance().CreateNxViewGroup(row,1,skin);
				ui.actorGroup.addEventListener(MouseEvent.CLICK,actorClickHandle,true);
//				ui.actorGroup.addItemsEvent(MouseEvent.ROLL_OVER,grouprollOver)
				ui.actorGroup.x=x;
				ui.actorGroup.y=y;
				pa.addChild(ui.actorGroup);
				initPartnerList();
				//重新初始化列表
//				refreshList(_role);
				for (var i:int = 0; i < _roleArr.length; i++) 
				{
					view=_roleArr[i];
					setMc(view);
				}
				currentSelectedM=ui.actorGroup.getItemByIndex(Math.min(selectIndex,_roleArr.length-1));
				return false;
			}
			setMc(view)
			return true;
			//				ui.actorGroup.row
			function setMc(viewObj:ObjectData):void
			{
				if(!viewObj)return ;
				var indexxx:int=uint(_roleArr.indexOf(view))
				var mc:MovieClip	=	ui.actorGroup.getItemByIndex(indexxx)
				mc[TXT].mouseEnabled	=	false;
				mc[TXT].text	=	viewObj is ViewObject?StaticIni.getSwfLangStr(viewObj.GetPropertiesEx("ConfigID")):viewObj.GetProperties("Name");
				if(viewObj.CheckProperties("Level")&&indexxx<=10)
				{
					if(mc[BIND_LEVEL]==null)
					{
						mc[BIND_LEVEL]=function (lvl:String):void{
							this[LVL].text	=	"lv."+(lvl&&lvl!="0"?lvl:1);
						}
					}
					//				if(viewObj.CheckProperties("Level")&&indexxx<=10)
					//				mc[BIND]=_blind.BindProp(viewObj,"Level",$this,"text"+indexxx);
					if(viewObj is RoleVo)
					{
						mc[BIND]	=_bind.BindPropInSceneObj(viewObj,"Level",BindFunc,null,[mc,BIND_LEVEL]);
					}
					else
					{
						mc[BIND]	=_bind.BindPropInViewObj(viewObj.parent as View,viewObj.ObjKey,"Level",BindFunc,null,[mc,BIND_LEVEL]);
					}
				}
				else
				{
					trace("Error,MyProperty，没有Level 这个属性！！！！！！！！！！！！！！！！！！！！！！！！！viewObj	=	",indexxx,viewObj);
				}
				mc[DATAOBJ]=viewObj;
				
			}
		}
	}
}